//
//  NSFCPopCell.m
//  Meboo
//
//  Created by kyo on 2024/4/29.
//

#import "NSFCPopCell.h"
@interface NSFCPopCell()
@property(nonatomic,strong) UILabel *moneyLab;
@property(nonatomic,strong) UILabel *titleLab;
@property (nonatomic, strong) UIButton *tipBtn;
@property (nonatomic, strong) CAGradientLayer *gradient;
@end

@implementation NSFCPopCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Public Metohd

- (void)setModel:(NSProductModel *)model {
    _model = model;
    self.moneyLab.text = [NSString stringWithFormat:@"$ %@",model.price];

    self.titleLab.text = [NSString stringWithFormat:@"%@ %@",model.benefitNum,model.benefitUnit];
    if ([model.tip isEqualToString:@"Popular"]) {
        self.tipBtn.hidden = NO;
        [self.tipBtn setImage:ImageWithName(@"home_pop_pp_Popular") forState:0];
    } else if ([model.tip isEqualToString:@"Preferential"]) {
        self.tipBtn.hidden = NO;
        [self.tipBtn setImage:ImageWithName(@"home_pop_pp_Preferential") forState:0];
    } else if (model.tip.length > 0) {
        [self.tipBtn setTitle:model.tip forState:0];
        self.tipBtn.hidden = NO;
    } else {
        self.tipBtn.hidden = YES;
    }
    [self.gradient removeFromSuperlayer];
    if (model.isSelect) {
        [self.contentView.layer insertSublayer:self.gradient atIndex:0];
    }
}

#pragma mark - Action


#pragma mark - UI
- (void)setupUI {
    self.contentView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:0.7];
    self.contentView.layer.cornerRadius = MBHeightScale(10);
    self.contentView.layer.masksToBounds = YES;
    
    [self.contentView addSubview:self.moneyLab];
    [self.moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(23));
        make.bottom.equalTo(self.contentView.mas_centerY).offset(MBHeightScale(-3));
    }];
    
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(23));
        make.top.equalTo(self.contentView.mas_centerY).offset(MBHeightScale(3));
    }];
    
    [self.contentView addSubview:self.tipBtn];
    [self.tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(MBHeightScale(23));
    }];
    
}

#pragma mark - Setter && Getter


- (UILabel *)moneyLab {
    if (!_moneyLab) {
        _moneyLab = [UILabel new];
        _moneyLab.font = MBHelBoldFontSizeScale(20);
        _moneyLab.textColor = HEXS(@"000000");
    }
    return _moneyLab;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.font = MBHelFontSizeScale(15);
        _titleLab.textColor = HEXS(@"000000");
    }
    return _titleLab;
}

- (UIButton *)tipBtn {
    if (!_tipBtn) {
        _tipBtn = [UIButton new];
        _tipBtn.hidden = YES;
        [_tipBtn setBackgroundImage:ImageWithName(@"home_fc_pop_back") forState:0];
        [_tipBtn setTitleColor:UIColor.whiteColor forState:0];
        _tipBtn.titleLabel.font = MBHelFontSizeScale(12);
        _tipBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 3);
    }
    return _tipBtn;
}

- (CAGradientLayer *)gradient {
    if (!_gradient) {
        //渐变色 设置开始和结束位置(设置渐变的方向)
       _gradient = [CAGradientLayer layer];
        _gradient.startPoint = CGPointMake(0, 0);
        _gradient.endPoint = CGPointMake(1, 1);
        _gradient.locations = @[@(0.0),@(0.46),@(1.0f)];
        _gradient.frame = self.contentView.bounds;
        _gradient.colors = [NSArray arrayWithObjects:(id)HEXS(@"FFD748").CGColor,(id)HEXS(@"FFC940").CGColor,(id)HEXS(@"FFC940").CGColor,nil];
    }
    return _gradient;
}
@end
